package com.easyfun.healthmagicbox.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPreferencesUtil {
	private static SharedPreferencesUtil instance = null;
	private SharedPreferences userinfo; 
	private Context mctx;
	private static Object syncRedoID = new Object();
	

//	private boolean remautologin;
//	private String STRING_AQ;
//	private String STRING_SW;
//	private String serverURL;
//	
//	private String APP_KEY;
//	private String APP_SECRET;
//    
//	private String ACCESS_KEY_NAME;
//	private String ACCESS_SECRET_NAME;
//    private String ACCESS_AUTH_TYPE_NAME;
//    private String ACCESS_UID_NAME;
//    private String SyncInterval;
//
//    private String storage_location;	
//	private Boolean KingDisk ; 

	Editor editor;
	
	
	public String getSyncInterval() {
		return userinfo.getString("SyncInterval","");
	}

	public void setSyncInterval(String syncInterval) {
		editor.putString("SyncInterval", syncInterval);
		editor.commit();
	}

	//where the head pic and pictures store. In external SD or inner storage
	public String getStorage_location() {
		return userinfo.getString("storage_location","");
	}

	public void setStorage_location(String storage_location) {
		editor.putString("storage_location", storage_location);
		editor.commit();
	}

	public String getUsername() {
		return userinfo.getString("APP_USERNAME","");
	}

	public void setUsername(String username) {
		editor.putString("APP_USERNAME", username);
		editor.commit();
	}

	public String getPersonID() {
		return userinfo.getString("APP_PERSONID","");
	}

	public void setPersonID(String personID) {
		editor.putString("APP_PERSONID", personID);
		editor.commit();
	}

	//whether the folder on kind cloud storage is created?
	public Boolean getKingDisk(String personID) {
		return userinfo.getBoolean(personID+"folder",false);
	}

	public void setKingDisk(String personID) {
		editor.putBoolean(personID+"folder", true);
		editor.commit();
	}

	public String getAPP_KEY() {
		return userinfo.getString("APP_KEY","");
	}

	public void setAPP_KEY(String aPP_KEY) {
		editor.putString("APP_KEY", aPP_KEY);
		editor.commit();
	}

	public String getAPP_SECRET() {
		return userinfo.getString("APP_SECRET","");
	}

	public void setAPP_SECRET(String aPP_SECRET) {
		editor.putString("APP_SECRET", aPP_SECRET);
		editor.commit();
	}

	public String getACCESS_KEY_NAME() {
		return userinfo.getString("ACCESS_KEY_NAME","");
	}

	public void setACCESS_KEY_NAME(String aCCESS_KEY_NAME) {
		editor.putString("ACCESS_KEY_NAME", aCCESS_KEY_NAME);
		editor.commit();
	}

	public String getACCESS_SECRET_NAME() {
		return userinfo.getString("ACCESS_SECRET_NAME","");
	}

	public void setACCESS_SECRET_NAME(String aCCESS_SECRET_NAME) {
		editor.putString("ACCESS_SECRET_NAME", aCCESS_SECRET_NAME);
		editor.commit();
	}

	public String getACCESS_AUTH_TYPE_NAME() {
		return userinfo.getString("ACCESS_AUTH_TYPE_NAME","");
	}

	public void setACCESS_AUTH_TYPE_NAME(String aCCESS_AUTH_TYPE_NAME) {
		editor.putString("ACCESS_AUTH_TYPE_NAME", aCCESS_AUTH_TYPE_NAME);
		editor.commit();
	}

	public String getACCESS_UID_NAME() {
		return userinfo.getString("ACCESS_UID_NAME","");
	}

	public void setACCESS_UID_NAME(String aCCESS_UID_NAME) {
		editor.putString("ACCESS_UID_NAME", aCCESS_UID_NAME);
		editor.commit();
	}

	public String getServerURL(String defaultvalue) {
		return userinfo.getString("serverURL",defaultvalue);
	}

	public void setServerURL(String serverURL) {
		editor.putString("serverURL", serverURL);
		editor.commit();
	}

	public boolean isRemautologin() {		
		return userinfo.getBoolean("remautologin", false);
	}

	public void setRemautologin(boolean remautologin) {
		editor.putBoolean("remautologin", remautologin);
		editor.commit();
	}

	public String getSTRING_AQ() {
		return userinfo.getString("STRING_AQ","");
	}

	public void setSTRING_AQ(String sTRING_AQ) {
		editor.putString("STRING_AQ", sTRING_AQ);
		editor.commit();
	}

	public String getSTRING_SW() {
		return userinfo.getString("STRING_SW","");
	}

	public void setSTRING_SW(String sTRING_SW) {
		editor.putString("STRING_SW", sTRING_SW);
		editor.commit();
	}

	public static SharedPreferencesUtil getInstance(Context ctx)
	{
		if (instance!=null)
			return instance;
		else
		{
			if (ctx == null)
			{
				return null;
			}else
			{
				return  new SharedPreferencesUtil(ctx);
			}
		}
			
	}
	
	private SharedPreferencesUtil(Context ctx)
	{
		this.mctx = ctx;
		userinfo = mctx.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
		editor = userinfo.edit();
	}
	
	public Integer getRedoTaskID()
	{
		Integer  redoid = 0;
		synchronized (syncRedoID)
		{
			redoid = userinfo.getInt("RedoTaskID",0);
			redoid ++;			
			editor.putInt("RedoTaskID", redoid );
			editor.commit();			
		}
		return redoid;
	}
	public void storeRedoTask(Integer redoID,String serializedTask)
	{
		editor.putString("RedoTask"+redoID.toString(), serializedTask );
		//increase redo task count
		synchronized (syncRedoID)
		{
			Integer redocount = userinfo.getInt("RedoTaskCount",0);
			redocount ++;			
			editor.putInt("RedoTaskCount", redocount );			
		}
		editor.commit();
	}
	public void removeRedoTask(Integer redoID) {
		editor.remove("RedoTask"+redoID.toString());		
		//decrease redo task count
		synchronized (syncRedoID)
		{
			Integer redocount = userinfo.getInt("RedoTaskCount",0);
			redocount --;			
			if (redocount < 0)
			{
				redocount = 0;
			}
			editor.putInt("RedoTaskCount", redocount );			
		}
		editor.commit();
	}
	
	
	public Map<Integer,String> getsRedoTasks() {

		Map<Integer,String> tasklist = new HashMap<Integer,String>();
		synchronized (syncRedoID)
		{
			Integer redocount = userinfo.getInt("RedoTaskCount",0);	
			Integer redoid = userinfo.getInt("RedoTaskID",0);
			for (int i=redoid; i >= 0; i--)
			{
				String redotask = userinfo.getString("RedoTask"+String.valueOf(i),"");	
				if (redotask.length()>0)
				{
					tasklist.put(Integer.valueOf(i),redotask);
					redocount--;
				}
				if (redocount<=0)
				{
					break;
				}
			}
		}
		return tasklist;
	}
}
